<template>
  <div>
    <t-table rowKey="id" size="small" :data="data" :columns="columns" hover :loading="dataLoading" :hideSortTips="true">
      <template #id="{ row, rowIndex }">
        {{ rowIndex + 1 }}
      </template>
      <template #[`reviewTeam.reviewType`]="{ row }">
        {{ row.reviewTeam.reviewType | filterByDict(dict.type.review_type) }}
      </template>
      <template #[`reviewTeam.subjectClassification`]="{ row }">
        {{ row.reviewTeam.subjectClassification | filterByDict(dict.type.subject_classification) }}
      </template>
      <template #opExpertAccountInfo="{ row }">
        <t-button @click="exportPDF(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="file-pdf" slot="icon" />
          导出
        </t-button>
      </template>
      <template #opExpertTransferCertificate="{ row }">
        <p v-if="row.showUploadTip" style="color: red">本组无需上传凭证</p>
        <file-upload
          @success="uploadSuccess"
          v-model="row.fileIds"
          :moduleKey="row.moduleKey"
          :fileList="row.fileLists"
          :multiple="true"
          v-else-if="createUserId === $store.state.user.userId"
        >
          <t-button
            size="small"
            variant="text"
            class="t-button-link"
            @click="currentRow = row"
          >
            <t-icon name="upload" slot="icon" />
            上传
          </t-button>
        </file-upload>
        <div v-else>
          <div v-if="row.fileLists" v-for="(item, index) in row.fileLists" :key="index">
            <a class="t-button-link" target="_blank" :href="item.url">
              {{ item.name }}
            </a>
          </div>
          <p v-else>-</p>
        </div>
      </template>
      <template #expertReviewInfo="{ row }">
        <t-button @click="openReviewDetail(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="root-list" slot="icon" />
          详情
        </t-button>
      </template>
    </t-table>

    <expert-review-list
      :rowId="dialog.rowId"
      :expert-leader="dialog.expertLeader"
      :teamId="dialog.teamId"
      :objId="dialog.objId"
      :reviewType="dialog.reviewType"
      :projectName="dialog.projectName"
      :visible.sync="dialog.open_review_list"
      :editOpen="false"
      v-if="dialog.open_review_list"
    ></expert-review-list>
    <expert-review-list-kjsf
      :rowId="dialog.rowId"
      :expert-leader="dialog.expertLeader"
      :teamId="dialog.teamId"
      :objId="dialog.objId"
      :reviewType="dialog.reviewType"
      :projectName="dialog.projectName"
      :visible.sync="dialog.open_review_list_kjsf"
      :editOpen="false"
      v-if="dialog.open_review_list_kjsf"
    ></expert-review-list-kjsf>
  </div>
</template>

<script>
import FileUpload from '@/components/FileUpload/index.vue';
import { getFiles, getFilesByModuleKey, getByModuleIdModuleKeyAndUserId } from '@/api/common';
import ExpertReviewList from '@/pages/kjxm/review/components/expert-review-list.vue';
import ExpertReviewListKjsf from '@/pages/kjsf/review/components/expert-review-list.vue';

export default {
  dicts: ['review_type', 'subject_classification'],
  components: {
    FileUpload,
    ExpertReviewList,
    ExpertReviewListKjsf,
  },
  props: {
    objId: [String, Number],
    createUserId: [String, Number],
    reviewOpen: {
      type: Boolean,
      default: false,
    },
    hideByProjectType: {
      type: Boolean,
      default: false,
    },
    isAdminOrKjc: {
      type: Boolean,
      default: false,
    },
    projectName: String,
    moduleName: {
      type: String,
      default: 'kjxm',
    },
  },
  data() {
    return {
      dataLoading: false,
      dialog: {
        open_review_list: false,
        open_review_list_kjsf: false,
      },
      currentRow: {},
      data: [],
      expertList: [],
      moduleApi: this.$api.kjxm,
    };
  },
  computed: {
    columns() {
      const columns = [
        {
          colKey: 'id',
          title: '序号',
          align: 'center',
          width: 64,
        },
        {
          colKey: 'reviewTeam.name',
          title: '评审组名称',
          align: 'center',
        },
        {
          colKey: 'reviewTeam.reviewType',
          title: '业务类型',
          align: 'center',
        },
        {
          colKey: 'reviewTeam.subjectClassification',
          title: '学科分类',
          align: 'center',
        },
        {
          colKey: 'reviewTeam.startReviewDate',
          title: '评审开始时间',
          align: 'center',
        },
        {
          colKey: 'reviewTeam.endReviewDate',
          title: '评审结束时间',
          align: 'center',
        },
        {
          colKey: 'opExpertAccountInfo',
          title: '专家账户信息',
          align: 'center',
        },
        {
          colKey: 'opExpertTransferCertificate',
          title: '专家转账凭证',
          align: 'center',
        },
      ];
      if (this.isAdminOrKjc) {
        const obj = {
          colKey: 'expertReviewInfo',
          title: '专家评审详情',
          align: 'center',
        };
        columns.push(obj);
      }
      return columns;
    },
  },
  watch: {
    moduleName: {
      immediate: true,
      handler(val) {
        if (val === 'kjsf') {
          this.moduleApi = this.$api.kjsf;
        }
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    reviewTeamConfigList() {
      this.$api.kjxm.project
        .reviewTeamConfigList()
        .then((res) => {
          this.data.forEach((row) => {
            if (res.data.find((item) => item.teamId === row.teamId)) {
              row.showUploadTip = true;
            }
          });
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    getList() {
      this.dataLoading = true;
      this.moduleApi.project
        .getTeamByBusinessId(this.objId)
        .then((res) => {
          this.data = res.data;
          this.reviewTeamConfigList();
          this.data.forEach((row) => {
            if (this.moduleName === 'kjsf') {
              if (row.reviewTeam.reviewType === '1') {
                row.moduleKey = 'sfprojectLXPSZJZZPZ';
              } else if (row.reviewTeam.reviewType === '2') {
                row.moduleKey = 'sfprojectSSPSZJZZPZ';
              } else if (row.reviewTeam.reviewType === '3') {
                row.moduleKey = 'sfprojectYSPSZJZZPZ';
              }
            } else if (this.moduleName === 'kjxm') {
              if (row.reviewTeam.reviewType === '1') {
                row.moduleKey = 'projectOLDLXPSZJZZPZ';
              } else if (row.reviewTeam.reviewType === '2') {
                row.moduleKey = 'projectDGPSZJZZPZ';
              } else if (row.reviewTeam.reviewType === '3') {
                row.moduleKey = 'projectYSPSZJZZPZ';
              } else if (row.reviewTeam.reviewType === '4') {
                row.moduleKey = 'projectLXPSZJZZPZ';
              } else if (row.reviewTeam.reviewType === '5') {
                row.moduleKey = 'projectLXPSZJZZPZ2';
              } else {
                console.log('未找到对应的reviewType，无法设置moduleKey');
              }
            }
            let id = this.$store.state.user.userId;
            if (
              this.$store.state.user.roles.includes('admin') ||
              this.$store.state.user.roles.includes('dwgly') ||
              this.$store.state.user.roles.includes('kjcspy')
            ) {
              id = this.createUserId;
            }
            // 回显附件
            // getByModuleIdModuleKeyAndUserId(row.reviewTeam.id, row.moduleKey, id)
            getFilesByModuleKey(row.id, row.moduleKey)
              .then((res) => {
                this.$set(row, 'fileLists', []);
                res.data.forEach((item) => {
                  row.fileLists.push({
                    name: item.fileName,
                    url: item.fullFilePath,
                    id: item.id,
                    filePath: item.filePath,
                  });
                });
              })
              .catch((e) => {
                this.$message.error(e.toString());
              });
          });
          this.dataLoading = false;
        })
        .catch((e) => {
          this.$message.error(e.toString());
          this.dataLoading = false;
        });
    },
    // 查专家列表
    teamExpertGet(id) {
      this.moduleApi.review
        .teamExpertGet(id)
        .then((response) => {
          this.expertList = response.data;
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
    async openReviewDetail(row) {
      this.dialog.objId = row.businessId;
      this.dialog.teamId = row.reviewTeam.id;
      this.dialog.projectName = this.projectName;
      await this.teamExpertGet(row.reviewTeam.id);
      // 先判断有没有设置专家组长
      let expertLeader = null;
      const obj = this.expertList.find((val) => val.teamLeader === '1');
      if (obj) {
        expertLeader = {
          id: obj.experts.userId,
          nickName: obj.experts.sysUser.nickName,
        };
      }
      this.dialog.expertLeader = expertLeader;
      this.dialog.rowId = row.id;
      this.dialog.reviewType = row.reviewTeam.reviewType;
      if (this.moduleName === 'kjsf') {
        this.dialog.open_review_list_kjsf = true;
      } else {
        this.dialog.open_review_list = true;
      }
    },
    exportPDF(row) {
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.moduleApi.project
        .createZjBankPdfByWindows(row.reviewTeam.id)
        .then((response) => {
          window.open(response.data.fullFilePath, '_blank');
          instance.hide();
        })
        .catch((e) => {
          instance.hide();
          this.$message.error(e.toString());
        });
    },
    uploadSuccess(ids) {
      this.moduleApi.project
        .updateInfo({
          id: this.currentRow.id,
          sysAttMainVo: {
            sysAttMainIds: ids,
          },
        })
        .then((val) => {
          this.$message.success('上传成功');
        })
        .catch((e) => {
          this.$message.error(e.toString());
        });
    },
  },
};
</script>